package com.thomas.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.thomas.entity.BlogType;
import com.thomas.entity.PageBean;
import com.thomas.service.BlogService;
import com.thomas.service.BlogTypeService;
import com.thomas.util.ResponseUtil;

/** 
 * @ClassName: BlogTypeAdminController 
 * @Description: 管理员博客类别Controller层 
 * @author thomas_liu  
 * @date 2017年9月4日 下午9:37:46 
 */
@Controller
@RequestMapping("/admin/blogType")
public class BlogTypeAdminController {
	
	@Autowired
	private BlogTypeService blogTypeService;
	
	@Autowired
	private BlogService blogService;
	
	/**
	 *分页查询博客类别信息
	 *@param page
	 *@param rows
	 *@param response
	 *@return
	 *@throws Exception 
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,HttpServletResponse response)throws Exception{
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<BlogType> blogTypeList=blogTypeService.list(map);
		Long total=blogTypeService.getTotal(map);
		JSONObject result = new JSONObject();
		JSONArray jsonArray= JSONArray.fromObject(blogTypeList);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 *添加或者修改博客类别信息
	 *@param blogType
	 *@param response
	 *@return
	 *@throws Exception  
	 */
	@RequestMapping("save")
	public String save(BlogType blogType,HttpServletResponse response)throws Exception{
		int resultTotal=0;//操作的记录条数
		if(blogType.getId()==null){
			resultTotal=blogTypeService.add(blogType);
		}else{
			resultTotal=blogTypeService.update(blogType);
		}
		JSONObject result=new JSONObject();
		if(resultTotal>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 *删除博客类别信息 
	 *@param ids
	 *@param response
	 *@return 
	 *@throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="ids")String ids, HttpServletResponse response)throws Exception{
		String[] idsStr=ids.split(",");
		JSONObject result = new JSONObject();
		for(int i=0;i<idsStr.length;i++){
			if(blogService.getBlogByTypeId(Integer.parseInt(idsStr[i]))>0){
				result.put("exist", "博客类别下有博客,不能删除!");
			}else{
				blogTypeService.delete(Integer.parseInt(idsStr[i]));
			}
		}
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
}
